package com.sys.controller;

import com.sys.common.aop.Operation;
import com.sys.common.constant.Constant;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import com.sys.pojo.Employee;
import com.sys.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Api(tags = {"员工管理"})
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/listAllEmployee")
    @RequiresPermissions("employee:list")
    public ResponseDateUtil getAllEmployee(@RequestBody Employee employee) {
        return ResultBuilder.success(employeeService.findListByPage(employee));
    }

    @PostMapping("/addEmployee")
    @RequiresPermissions("employee:add")
    @Operation("添加新员工")
    @ApiOperation("添加新员工")
    public ResponseDateUtil addEmployee(@RequestBody Employee employee) {
        if (employeeService.add(employee) > 0) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }

    @PutMapping("/updEmployee")
    @RequiresPermissions("employee:upd")
    @Operation("更新员工信息")
    @ApiOperation("更新员工信息")
    public ResponseDateUtil updEmployee(@RequestBody Employee employee) {
        if (employeeService.updateData(employee) > 0) {
            return ResultBuilder.success(Constant.UPDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.UPDFAILED);
        }
    }

    @DeleteMapping("/delEmployee")
    @RequiresPermissions("employee:del")
    @Operation("删除员工信息")
    @ApiOperation("删除员工信息")
    public ResponseDateUtil delEmployee(@RequestParam("id") Integer id) {
        if (employeeService.delete(id) > 0) {
            return ResultBuilder.success(Constant.DELSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.DELFAILED);
        }
    }
}
